import { Application } from 'egg';
import utils from "~/utils";

export default function (app: Application) {
    const { DataTypes } = app.Sequelize;

    const modelName = 'users';

    const Users = app.model.define(modelName, {
        id: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: false,
            defaultValue: null,
            primaryKey: true,
            autoIncrement: true,
            comment: "主键ID",
            field: "id"
        },
        name: {
            type: DataTypes.STRING(255),
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: "姓名",
            field: "name"
        },
        age: {
            type: DataTypes.INTEGER,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: "年龄",
            field: "age"
        },
        created_at: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: "创建时间",
            field: "created_at",
            get() {
                const rawValue = this.getDataValue('created_at');
                return utils.text.toFormatTime(rawValue);
            }

        },
        updated_at: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: "更新时间",
            field: "updated_at",
            get() {
                const rawValue = this.getDataValue('updated_at');
                return utils.text.toFormatTime(rawValue);
            }
        }
    }, {
        tableName: app.config.databaseSchema + modelName
    });

    return class extends Users {
        // static associate() {
        //     app.model.Users.hasMany(app.model.Post, { as: 'posts' });
        // }
    }
}